<template>
  <div class="header-container">
    <div class="header-left flex-box">
      <el-icon class="icon" size="20" @click="store.commit('collapseMenu')">
        <Fold />
      </el-icon>
      <ul class="flex-box">
        <li
          v-for="(item, index) in selectMenu"
          :key="item.id"
          class="tab flex-box"
          :class="{ selected: route.path === item.path }"
        >
          <el-icon size="12">
            <component :is="item.icon" />
          </el-icon>
          <router-link class="text flex-box" :to="{ path: item.path }">
            {{ item.name }}
          </router-link>
          <el-icon class="close" size="12" @click="closeTab(item, index)">
            <Close />
          </el-icon>
        </li>
      </ul>
    </div>
    <div class="header-right">
      <el-dropdown @command="handleCommand">
        <span class="el-dropdown-link flex-box">
          <el-avatar
            :src="pz_userInfo.avatar || 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png'"
          />
          <p class="name-box">{{pz_userInfo.name || 'admin'}}</p>
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item command="cancel">退出</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<script setup>
import { useStore } from "vuex";
import { computed } from "vue";
import { useRoute, useRouter } from "vue-router";

const pz_userInfo = JSON.parse(localStorage.getItem('pz_userInfo') || {})

const store = useStore();
const selectMenu = computed(() => store.state.menu.selectMenu);

const route = useRoute();
const router = useRouter()

const closeTab = (item, index) => {
  store.commit('closeMenu', item)
  // 删除的非当前页tab
  if (route.path !== item.path) {
    return;
  }
  const selectMenuData = selectMenu.value;
  // 删除的最有一项
  if (index === selectMenuData.length) {
    // 如果tab只有一个元素
    if (!selectMenuData.length) {
      router.push('/')
    } else {
      router.push({
        path: selectMenuData[index - 1].path
      })
    }
  } else {
    // 如果删除的是中间位置的tag
    router.push({
      path: selectMenuData[index].path
    })
  }
}

const handleCommand = (command) => {
  if (command === 'cancel') {
    localStorage.clear()
    // window.location.href = window.location.origin
    router.replace('/login')
  }
}


</script>

<style lang="less" scoped>
.flex-box {
  display: flex;
  align-items: center;
  height: 100%;
}
.header-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
  background-color: #fff;
  padding-right: 25px;

  .header-left {
    height: inherit;
    .icon {
      width: 45px;
      height: 100%;
    }
    .icon:hover {
      background-color: #f5f5f5;
      cursor: pointer;
    }
    .tab {
      padding: 0 10px;
      height: 100%;
      .text {
        margin: 0 5px;
        color: #000;
        font-size: 14px;
        text-decoration: none;
      }
      .close {
        visibility: hidden;
      }
    }
    .tab {
      &:hover {
        background-color: #f5f5f5;
        .close {
          visibility: inherit;
          cursor: pointer;
          color: #000;
        }
      }
      &.selected {
        a {
          color: #409eff;
        }
        i {
          color: #409eff;
        }
        background: #f5f5f5;
      }
    }
  }

  .header-right {
    cursor: pointer;

    .name-box {
      padding-left: 10px;
    }
    .el-dropdown-link:focus-visible {
      outline: none;
    }
  }
}
</style>